package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Goal;
import util.DBHelp;
import util.RowMapper;

public class GoalDao {

	private DBHelp<Goal> db=new DBHelp<Goal>();
	
	public void save(Goal goal){
		String sql="insert into t_goal (id,title,content,createtime,user_id,project_id) values(?,?,?,?,?,?)";
		db.executeSQL(sql, goal.getId(),goal.getTitle(),goal.getContent(),goal.getCreatetime(),goal.getUser_id(),goal.getProject_id());
	}
	
	public Goal findGoalById(String id){
		String sql="select id,title,content,createtime,user_id,project_id from t_goal where id=?";
		return db.executeQueryToObject(sql, new GoalRowMapper(),id);
	}
	
	public class GoalRowMapper implements RowMapper<Goal>{

		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal=new Goal();
			
			goal.setId(rs.getString("id"));
			goal.setTitle(rs.getString("title"));
			goal.setContent(rs.getString("content"));
			goal.setCreatetime(rs.getString("createtime"));
			goal.setUser_id(rs.getString("user_id"));
			goal.setProject_id(rs.getString("project_id"));
			
			
			return goal;
			
		}
		
	}

	public List<Goal> findGoalAll() {
		String sql="select id,title,content,createtime,user_id,project_id from t_goal order by createtime desc";
		return db.executeQueryToList(sql,new GoalRowMapper());
	}

	public void update(Goal goal) {
		String sql="update t_goal set title=?,content=? where id=?";
		db.executeSQL(sql,goal.getTitle(),goal.getContent(),goal.getId());
	}
}
